package dao;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import bean.Admin;
import bean.News;
import util.JDBCUtil;

/**
 * 
 * AdminDAO类：访问数据库以返回相关管理员信息
 *
 */
public class AdminDAO {
	private Connection conn = null;//数据库连接
	private static final int PAGE_NEWS_SIZE = 7;
	public AdminDAO() {

	}

	/**
	 * 
	 * @param account 账号
	 *
	 * @return Admin 管理员信息
	 */
	public Admin getAdmin(String account) {
		Admin admin = new Admin();
		PreparedStatement preStatement = null;
		ResultSet res = null;

		try {
			this.conn = JDBCUtil.getConn();
			preStatement = this.conn.prepareStatement("select * from admin where AdminAccont = ?");
			preStatement.setString(1, account);
			res = preStatement.executeQuery();
			res.next();
			admin.setAccount(res.getString("AdminAccont"));
			admin.setPwd(res.getString("AdminPassword"));
			admin.setName(res.getString("AdminName"));
			admin.setSex(res.getString("AdminSex"));
			admin.setId(res.getInt("id"));
			admin.setAdmin(res.getBoolean("isAdmin"));
			return admin;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.closeResultSet(res);
			JDBCUtil.closeStatement(preStatement);
			JDBCUtil.closeConn(this.conn);
		}
		return null;
	}




	/**
	 *
	 * @param account 账号
	 *
	 * @return  添加普通成员信息
	 */
	public boolean addAdmin(Admin account) {
		PreparedStatement preStatement = null;
		ResultSet res = null;

		try {
			this.conn = JDBCUtil.getConn();
			preStatement = this.conn.prepareStatement("insert admin values(?, ?, ?, ?, ?, ?)");
			preStatement.setInt(1, account.getId());
			preStatement.setString(2, account.getAccount());
			preStatement.setString(3, account.getPwd());
			preStatement.setString(4, account.getName());
			preStatement.setString(5, account.getSex());
			preStatement.setBoolean(6, account.getAdmin());
			int cnt = preStatement.executeUpdate();
			if (cnt > 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.closeStatement(preStatement);
			JDBCUtil.closeConn(this.conn);
		}
		return false;
	}

	public boolean deleteAdmin(int id) {
		PreparedStatement preStatement = null;
		try {
			conn = JDBCUtil.getConn();
			preStatement = conn.prepareStatement("delete from admin where id =?");
			preStatement.setInt(1, id);
			int result = preStatement.executeUpdate();
			if (result > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.closeStatement(preStatement);
			JDBCUtil.closeConn(conn);
		}
		return false;
	}

	/**
	 * 更新信息
	 *
	 * @param admin 包含更新对象
	 * @return 是否更新成功
	 */
	public boolean updateAdmin(Admin admin) {
		PreparedStatement preStatement = null;
		try {
			conn = JDBCUtil.getConn();
			String sql = "update admin set AdminAccont =?, AdminPassword =?, AdminName =?, AdminSex =?, isAdmin =? where id =?";
			preStatement = conn.prepareStatement(sql);
			preStatement.setString(1, admin.getAccount());
			preStatement.setString(2, admin.getPwd());
			preStatement.setString(3, admin.getName());
			preStatement.setString(4, admin.getSex());
			preStatement.setBoolean(5, admin.getAdmin());
			preStatement.setInt(6, admin.getId());
			int result = preStatement.executeUpdate();
			if (result > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.closeStatement(preStatement);
			JDBCUtil.closeConn(conn);
		}
		return false;
	}
	public int getAllRecords() {
		PreparedStatement preStatement = null;
		ResultSet res = null;

		try {
			this.conn = JDBCUtil.getConn();
			preStatement = this.conn.prepareStatement("select count(*) 'cnt' from admin");
			res = preStatement.executeQuery();
			res.next();
			return res.getInt("cnt");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.closeResultSet(res);
			JDBCUtil.closeStatement(preStatement);
			JDBCUtil.closeConn(this.conn);
		}
		return 0;
	}
	public int getTotalPage(int totalRow) {
		int totalPage = 0;

		if (totalRow % PAGE_NEWS_SIZE == 0) {
			totalPage = totalRow / PAGE_NEWS_SIZE;
		} else {
			totalPage = totalRow / PAGE_NEWS_SIZE + 1;
		}
		return totalPage;
	}

	public List<Admin> getCurrentPageNews(int page) {


			PreparedStatement preStatement = null;
			ResultSet res = null;
			int position = 0;
			int counter = 0;
			List<Admin> newsList = new ArrayList<>();

			try {
				int totalRow = getAllRecords();
				int totalPage = getTotalPage(totalRow);
				position = (page - 1) * PAGE_NEWS_SIZE;
				if (page != totalPage) {
					counter = PAGE_NEWS_SIZE;
				} else {
					counter = Integer.MAX_VALUE;
				}
				this.conn = JDBCUtil.getConn();
				preStatement = this.conn.prepareStatement("select * from admin  limit ?, ?");
				preStatement.setInt(1, position);
				preStatement.setInt(2, counter);
				res = preStatement.executeQuery();
				while (res.next()) {
					Admin news = new Admin();
					news.setId(res.getInt("id"));
					news.setAccount(res.getString("AdminAccont"));
					news.setName(res.getString("AdminName"));
					news.setPwd(res.getString("AdminPassword"));
					news.setSex(res.getString("AdminSex"));
					news.setAdmin(res.getBoolean("isAdmin"));
					newsList.add(news);
				}
				return newsList;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				JDBCUtil.closeResultSet(res);
				JDBCUtil.closeStatement(preStatement);
				JDBCUtil.closeConn(this.conn);
			}
			return null;

	}
}
